import validateRule from './validateRule';
// 核心 自动prop组合 for validate验证
function autoValidate (option, cbs) {
  // function autoValidateRule(string, model) {
  //     return function (rule, value, callback) {
  //         if ((new RegExp(string, "g")).exec(value)) {
  //             return callback(new Error(model));
  //         }
  //         else {
  //             return callback();
  //         }
  //     };
  // }
  let temp = Object.create(null);
  let i = 0;
  for (let index in option) {
    if (!option.hasOwnProperty(index)) continue;
    if (!option[index]) continue;
    let type = option[index].constructor;
    switch (type) {
    case Array:
      temp[index] = [];
      for (let b in option[index]) {
        if (!option[index].hasOwnProperty(b)) continue;
        if (!option[index][b]) continue;
        if (option[index][b].constructor === String) {
          if (option[index][b].indexOf('__') !== -1) {
            let step = option[index][b].split('__');
            temp[index].push({validator: validateRule[step[0]], trigger: step[1]});
          } else {
            temp[index].push({validator: validateRule[option[index][b]], trigger: 'blur'});
          }
        } else if (option[index][b].constructor === Object) {
          if (typeof option[index][b].val !== 'undefined') {
            let step = {
              ...option[index][b],
              validator: validateRule[option[index][b].val],
            };
            delete step.val;
            temp[index].push(step);
          } else {
            temp[index].push(option[index][b]);
          }
        }
      }
      break;
    case String:
      if (!option[index]) {
        temp[index] = [{validator: cbs[i++], trigger: 'blur'}];
      } else {
        if (option[index].indexOf('__') !== -1) {
          let step = option[index].split('__');
          temp[index] = [{validator: validateRule[step[0]], trigger: step[1]}];
        } else {
          temp[index] = [{validator: validateRule[option[index]], trigger: 'blur'}];
        }
      }
      break;
    case Object:
      temp[index] = [option[index]];
      break;
    }
  }
  return temp;
  /* autoValidate({
   pass:'',
   imsunhao:'vNull',
   name:['vNull','a']
   }, [
   function(rule, value, callback){
   console.log(123)
   }
   ]); */
}
/* // 核心 自动obj组合 for validate验证
function autoValidatePuls (obj) {
  for (let index in obj) {
    if (obj.hasOwnProperty(index)) {
      obj[index] = autoValidate(obj[index]);
    }
  }
  return obj;
} */

export default option => {
  let {
    data,
  } = option;
  let computedStep = {};
  for (let formRef in data) {
    if (data.hasOwnProperty(formRef) && /^form/.test(formRef)) {
      let formValue = {};
      let formRule = {};
      let formType = {};
      let formDictionary = {};
      for (let formItem in data[formRef]) {
        if (data[formRef].hasOwnProperty(formItem)) {
          if (data[formRef][formItem].constructor === Object) {
            let {
              value,
              rule,
              type,
              dictionary,
            } = data[formRef][formItem];
            formValue[formItem] = value;
            formRule[formItem] = rule;
            formType[formItem] = type;
            if (dictionary) formDictionary[formItem] = dictionary;
          } else {
            formValue[formItem] = data[formRef][formItem];
          }
        }
      }
      data[formRef] = {
        ...formValue,
        ...{
          formRule: autoValidate(formRule),
          formType,
          formRef,
        },
      };
      computedStep['autoVue_' + formRef] = function () {
        let type = Object.assign({}, this[formRef].formType);
        let step = Object.assign({}, this[formRef]);
        delete step.formRef;
        delete step.formRule;
        delete step.formType;
        let option = {};
        for (let index in step) {
          if (step.hasOwnProperty(index)) {
            switch (type[index]) {
            case 'Date':
              option[index] = new Date(step[index]).getTime();
              break;
            case 'dateRange':
            case 'DateRange':
              option[index + 'ks'] = new Date(step[index][0]).getTime();
              option[index + 'js'] = new Date(step[index][1]).getTime();
              break;
            default:
              option[index] = step[index];
            }
          }
        }
        return option;
      };
      computedStep['autoVueDictionary_' + formRef] = function () {
        let autoVue = this['autoVue_' + formRef];
        let step = {};
        for (let index in formDictionary) {
          if (formDictionary.hasOwnProperty(index)) {
            step[index] = autoVue[index];
          }
        }
        return step;
      };
    }
  }
  option.computed = {
    ...option.computed,
    ...computedStep,
  };
  option.methods = {
    ...option.methods,
    ...{
      VALIDATE: function (formRef, success, DICTIONARY) {
        if (this.$refs[formRef]) {
          this.$refs[formRef].$refs[formRef].validate(valid => {
            if (valid) {
              if (success) {
                success.call(
                  this,
                  this.FORMOPTION(formRef),
                  DICTIONARY ? this.DICTIONARY(DICTIONARY, this.FORMDICTIONARY(formRef)) : '');
              }
            } else {
              return false;
            }
          });
        }
      },
      resetFields: function (formRef) {
        if (this.$refs[formRef]) {
          this.$refs[formRef].$refs[formRef].resetFields();
        }
      },
      FORMOPTION: function (formRef) {
        return this['autoVue_' + formRef];
      },
      FORMDICTIONARY: function (formRef) {
        return this['autoVueDictionary_' + formRef];
      },
    },
  };
};
